package com.acane.instagram.feature.chat

import androidx.compose.runtime.remember
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
import com.acane.instagram.feature.friend.ChatViewModelFactory
import com.acane.instagram.viewmodel.ChatViewModel

const val CHAT_ROUTE = "home"

const val CHAT_USER = "user_name"

fun NavGraphBuilder.chatScreen(
    navController: NavHostController,
    toProfile: (String) -> Unit,
) {
    composable("${CHAT_ROUTE}/{${CHAT_USER}}") { backStackEntry ->
        val userName = backStackEntry.arguments?.getString(CHAT_USER) ?: ""
        val savedStateHandle = remember { SavedStateHandle(mapOf(CHAT_USER to userName)) }
        val chatViewModel: ChatViewModel = viewModel(factory = ChatViewModelFactory(savedStateHandle))
        ChatRoute(
            chatViewModel = chatViewModel,
            navController = navController,
            toProfile = toProfile,
        )
    }
}
fun NavController.navigateToChat(userName: String): Unit {
    navigate("${CHAT_ROUTE}/$userName")
}
